package net.ariel.myideablog.web.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.ariel.myideablog.dao.ILink;
import net.ariel.myideablog.impl.LinkImpl;
import net.ariel.myideablog.model.Link;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class LinkController extends MultiActionController {

	private String viewPage;
	private ILink iLink;
	private String errorString;
	private int numberPerPage;
	private int pages;
	private Link link;
	
	public void setViewPage(String viewPage) {
		this.viewPage = viewPage;
	}
	
	public void setILink(ILink iLink) {
		this.iLink = iLink;
	}
	
	public void setErrorString(String errorString) {
		this.errorString = errorString;
	}
	
	public void setNumberPerPage(int numberPerPage) {
		this.numberPerPage = numberPerPage;
	}
	
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
		int linkNumber = iLink.getLinkNumber();
		if(numberPerPage != 0) {
			pages = linkNumber/numberPerPage;
			if(linkNumber % numberPerPage != 0) {
				pages++;
			}
		}
		else {
			pages = 0;
		}
		List<Link> linkList = iLink.getLinksByPage(0, numberPerPage);
		ModelAndView mav = new ModelAndView();
		mav.setViewName(viewPage);
		mav.addObject("links", linkList);
		mav.addObject("pages", pages);
		mav.addObject("link", link);
		mav.addObject("error", errorString);
		return mav;
	}
	
	public ModelAndView page(HttpServletRequest request, HttpServletResponse response) {
		String page = request.getParameter("page");
		int pageNo = Integer.parseInt(page);
		List<Link> linkList = iLink.getLinksByPage(pageNo*numberPerPage, (pageNo+1)*numberPerPage);
		int linkNumber = iLink.getLinkNumber();
		if(numberPerPage != 0) {
			pages = linkNumber/numberPerPage;
			if(linkNumber % numberPerPage != 0) {
				pages++;
			}
		}
		else {
			pages = 0;
		}
		ModelAndView mav = new ModelAndView();
		mav.setViewName(viewPage);
		mav.addObject("links", linkList);
		mav.addObject("pages", pages);
		return mav;
	}
	
	public String add(HttpServletRequest request, HttpServletResponse response) {
		String title = request.getParameter("title");
		String url = request.getParameter("url");
		if(title == null || title.trim().isEmpty() || url == null || url.trim().isEmpty()) {
			setErrorString("Empty link title or link url");
		}
		else {
			Link link = new Link(title, url);
			iLink.saveLink(link);
			setErrorString(null);
		}
		return "redirect:/admin/link.do";
	}
	
	public String edit(HttpServletRequest request, HttpServletResponse response) {
		String key = request.getParameter("key");
		if(key != null) {
			link = iLink.getLink(key);
		}
		return "redirect:/admin/link.do";
	}
	
	public String update(HttpServletRequest request, HttpServletResponse response) {
		String key = request.getParameter("key");
		String title = request.getParameter("title");
		String url = request.getParameter("url");
		if(title == null || title.trim().isEmpty() || url == null || url.trim().isEmpty()) {
			setErrorString("Empty link title or url");
		}
		else {
			Link newLink = new Link();
			newLink.setTitle(title);
			newLink.setUrl(url);
			iLink.updateLink(key, newLink);
			link = null;
		}
		return "redirect:/admin/link.do";
	}
	
	public String delete(HttpServletRequest request, HttpServletResponse response) {
		String linkKey = request.getParameter("key");
		iLink.deleteLink(linkKey);
		return "redirect:/admin/link.do";
	}
}
